package com.etlmaster.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.XmlWebApplicationContext;

import com.etlmaster.executor.utils.LogWriter;

/**
 * @author Larry
 * @copyright Larry@etlMaster
 * @since 2017-09-10
 */
@Component
public class BeanFactory implements ApplicationContextAware{

	protected static ApplicationContext applicationContext;
	
	private static Logger LOG = LoggerFactory.getLogger(BeanFactory.class);
	
	protected static DefaultListableBeanFactory defaultListableBeanFactory;
	
	public static Object getBean(String name){
		if(applicationContext!=null){
			return applicationContext.getBean(name);
		}
		LOG.warn("Spring容器为空，无法获取beanName：{}",name);
		return null;
	}
	
	public static <T> T getBean(Class<T> cls){
		if(applicationContext!=null){
			return applicationContext.getBean(cls);
		}
		LOG.warn("Spring容器为空，无法获取beanClass：{}",cls);
		return null;
	}
	
	public static void registerBeanDefinition(String beanId,GenericBeanDefinition beanDefinition,String[] beanAlias){
		defaultListableBeanFactory.registerBeanDefinition(beanId,beanDefinition);
		for (int j = 0;beanAlias!=null && beanAlias.length>0 && j < beanAlias.length; j++) {
			defaultListableBeanFactory.registerAlias(beanId,beanAlias[j]);
		}
	}
	
	public static BeanDefinition getBeanDefinition(String beanName){
		return defaultListableBeanFactory.getBeanDefinition(beanName);
	}
	
	public void setApplicationContext(ApplicationContext applicationContext1)throws BeansException {
		this.applicationContext=applicationContext1;		
		//1.加载配置文件
		Configuration.getInstance().loadClasspathProperties(applicationContext1);
		ConfigurableListableBeanFactory beanFactory=null;
		if(applicationContext instanceof GenericApplicationContext){//架包中的配置文件
			beanFactory = ((GenericApplicationContext)applicationContext).getBeanFactory();
		}else if(applicationContext instanceof FileSystemXmlApplicationContext){//文件系统的配置文件
			beanFactory = ((FileSystemXmlApplicationContext)applicationContext).getBeanFactory();
		}else if(applicationContext instanceof XmlWebApplicationContext){//web的配置文件
			beanFactory=((XmlWebApplicationContext)applicationContext).getBeanFactory();
		}else{
			LogWriter.addLog("warn","applicationContext的类型没有识别成功,为：{}",applicationContext1.getClass().getName());
		}
		
		if(beanFactory instanceof DefaultListableBeanFactory){
			defaultListableBeanFactory = (DefaultListableBeanFactory)beanFactory;
		}else{
			LogWriter.addLog("warn","beanFactory的类型没有识别成功，为：{}",beanFactory.getClass().getName());
		}
	}
}

